
## set working directory for the data
setwd("")

library(foreign)
history <- read.dta("data.dta")
library(mediation)
library(stargazer)

## publicity

model1.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_1 > -1))
model1.y <- glm(q100_1 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_1 > -1))

out.1 <- mediate(model1.m, model1.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.1)

sens.1 <- medsens(out.1, rho.by = 0.05)
summary(sens.1)
par.orig1 <- par(mfrow = c(2,2))
plot(sens.1, sens.par = "rho")

plot(sens.1, sens.par = "R2", r.type = "total", sign.prod = -1)
par(par.orig1)

## sanction

model2.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_2 > -1))
model2.y <- glm(q100_2 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_2 > -1))

out.2 <- mediate(model2.m, model2.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.2)

sens.2 <- medsens(out.2, rho.by = 0.05)
summary(sens.2)
par.orig2 <- par(mfrow = c(2,2))
plot(sens.2, sens.par = "rho")

plot(sens.2, sens.par = "R2", r.type = 2, sign.prod = -1)
par(par.orig2)

## Compromise through negotiation


model3.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_4 > -1))
model3.y <- glm(q100_4 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_4 > -1))

out.3 <- mediate(model3.m, model3.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.3)

sens.3 <- medsens(out.3, rho.by = 0.05)
summary(sens.3)
par.orig3 <- par(mfrow = c(2,2))
plot(sens.3, sens.par = "rho")

plot(sens.3, sens.par = "R2", r.type = 2, sign.prod = -1)
par(par.orig3)

## IO arbitration


model4.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_5 > -1))
model4.y <- glm(q100_5 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_5 > -1))

out.4 <- mediate(model4.m, model4.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.4)

sens.4 <- medsens(out.4, rho.by = 0.05)
summary(sens.4)
par.orig4 <- par(mfrow = c(2,2))
plot(sens.4, sens.par = "rho")

plot(sens.4, sens.par = "R2", r.type = 2, sign.prod = -1)
par(par.orig4)


## Joint development


model5.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_6 > -1))
model5.y <- glm(q100_6 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_6 > -1))

out.5 <- mediate(model4.m, model4.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.5)

sens.5 <- medsens(out.5, rho.by = 0.05)
summary(sens.5)
par.orig5 <- par(mfrow = c(2,2))
plot(sens.5, sens.par = "rho")

plot(sens.5, sens.par = "R2", r.type = 2, sign.prod = -1)
par(par.orig5)


## Military action


model6.m <- lm(hardcore4 ~historical + nationalism + powerful + valuable + island 
               + age + han + male + eastern + central + rural + college + SOE + ccp 
               + income + socialstatus + news + defense, data=subset(history, q100_7 > -1))
model6.y <- glm(q100_7 ~ hardcore4 + historical + nationalism + powerful + valuable + island 
                + age + han + male + eastern + central + rural + college + SOE + ccp 
                + income + socialstatus + news + defense, family = binomial(link = "probit"), 
                data=subset(history, q100_7 > -1))

out.6 <- mediate(model6.m, model6.y, sims = 1000, treat = "historical", mediator = "hardcore4")

summary(out.6)

sens.6 <- medsens(out.6, rho.by = 0.05)
summary(sens.6)
par.orig6 <- par(mfrow = c(2,2))
plot(sens.6, sens.par = "rho")

plot(sens.6, sens.par = "R2", r.type = 2, sign.prod = -1)
par(par.orig6)

##plotting

##Figure 4

fig <- par(mfrow = c(3,2))
plot.mediate(out.1, main="Publicity")
plot.mediate(out.2, main="Economic Sanction")
plot.mediate(out.3, main="Compromise")
plot.mediate(out.4, main="IO Arbitration")
plot.mediate(out.5, main="Joint Development")
plot.mediate(out.6, main="Military Action")

par(fig)

##Appendix H

fig2 <- par(mfrow = c(4,2))
plot(sens.3, sens.par = "rho", main="Compromise")
plot(sens.4, sens.par = "rho", main="IO Arbitration")
plot(sens.5, sens.par = "rho", main="Joint Development")
plot(sens.6, sens.par = "rho", main="Military Action")

par(fig2)

####Table 3 using IRT measure of hardcore (Appendx G)

stargazer(model1.y, model2.y, model3.y, model4.y, model5.y, model6.y, out="table3IRT.tex")



